mini.parse();
var grid=mini.get("datagrid1");
var searchCols=mini.get("searchCols");//获得下拉框的ID
var searchKey=mini.get("searchKey")
$(function () {
    request();
})
function onKeyEnter(e) {
    search();
}
function request() {
    $.ajax({
        url: ajaxUrl + "/sqlAction/showTask.do",
        type: "post",
        dataType: "json",
        contentType : "application/json",
        success: function (data) {
            console.log(data);
                grid.setData(data);
            searchCols.setData(data);
        },
        error: function (data) {
        }
    });
}

function oncanCel() {
    var row = grid.getSelecteds();
    console.log(row);
    if (row.length > 1) {
        mini.alert('不能对多条数据进行撤销');
    } else if (row.length == 0) {
        mini.alert('请选择需要撤销的行');
    } else {
        var cTableName = row[0].cTableName;
        $.ajax({
            url: ajaxUrl + '/activitiTaskController/cancelMyTask.do',
            type: 'POST',
            data: {
                cTableName:cTableName,
                id: row[0][cTableName + '_id']
            },
            success: function (data) {
                console.log(data);
                mini.alert(data.data);
            },
            error: function (data) {
                console.log(data);
                mini.alert("失败");
            }
        })
    }
}
function onSubmit(){
    var row = grid.getSelected();
    var cTableName = row.cTableName;
    console.log(row);
    console.log(row[cTableName + '_id']);
    console.log(row[cTableName+'_single_person']);
    var state;
    if(row){
        $.ajax({
            url : ajaxUrl+"/activitiTaskController/findStateById.do",
            data: {
                id:row[cTableName + '_id'],
                cTableName:row.cTableName
            },
            type : 'post',
            success: function(data){
                console.log(data);
                state = data.state[0][cTableName+'_state'];
                console.log(data.state);
                console.log(state);


                if(state == 1||state==2|| state==3){
                    mini.alert('不能重复提交');
                }else{
                    console.log('能提交');
                    $.ajax({
                        url : ajaxUrl+"/activitiTaskController/startApply.do",
                        data: {
                            taskId: row[cTableName+'_id'],
                            formId: row.formId,
                            userName:row[cTableName+'_single_person']
                        },
                        type : 'post',
                        success: function(data){
                            mini.alert(data.msg);
                            // var taskId = data.taskId;
                            // var msg = '消息有新任务|' + userInfoObj.userId+'|'+data.userInfo.userId;
                            // console.log(msg, '要发送的消息');
                            // send(msg);
                        },
                        error: function(data){
                            console.log(data);
                        }
                    })
                }
            },
            error: function(data){
                console.log(data);
            }
        });
    }else{
        mini.alert('请选择需要提交的数据');
    }
}
// 查看
function onSee(){
    var row = grid.getSelected();
    var clickStatus = 'rowdblclick';
    var cTableName = row.cTableName;
     if(row){
        //请求编辑数据的参数
         var obj = {
             cTableName: row.cTableName,// 主表的名称
             formId: row.formId,// 表单id
             id: row[row.cTableName + '_id'],
         };
         if(row.mTableId == undefined) {
             obj.mTableId = row[cTableName+'_id'];
         }else {
             obj.mTableId = row.mTableId;
         }
         //添加缓存数据，子页面获取值
         localStorage.setItem("obj",JSON.stringify(obj));
         //请求编辑数据,并跳转页面
         $.ajax({
             //url: "http://192.168.31.65:8080/bpm/formltems/formByMtableId.do",
             url: ajaxUrl + "/sqlAction/findById.do",
             type: "post",
             dataType: "json",
             contentType : "application/json",
             data: JSON.stringify(obj),
             success: function (data) {
                 console.log(data, '获得编辑的数据');
                 formShow = {//表单展示页
                     action: 'edit',
                     formListId: row.formId,
                     cTableName: cTableName,
                     // menuId: menuId,
                     edit_data: data[0],
                     // treeSelected: treeSelected,
                     // textField: textField,
                     // complex_add_field: complex_add_field,
                     formState: row[cTableName + '_state'],
                     clickStatus: clickStatus
                 };
                 localStorage.setItem("formShow",JSON.stringify(formShow));
                 var formSh = localStorage.getItem("formShow");
                 var  formSh=$.parseJSON(formSh);
                 console.log(formSh);
                 var item = {
                     iconCls: "fa fa-desktop",
                     // id: formId,
                     text: "表单展示",
                     url: "../pages/formDesign/formTemplate2.html"
                 };
                 parent.activeTab(item);//与父页面通信
             },
             error: function (data) {
             }
         });
     }else{
         mini.alert("请至少选择一条数据查看");
     }
}
function search() {
    var colVal = searchCols.getValue();
    var keyVal = searchKey.getValue();
    if(colVal){
        if(keyVal){
            var searchObj = {
                colVal: colVal,
                keyVal: keyVal,
            };
            var jsonStr = JSON.stringify(searchObj);
            console.log(jsonStr);
            $.ajax({
                url: ajaxUrl + '/sqlAction/findAllTaskByCondition.do',
                type: 'POST',
                data: jsonStr,
                contentType : "application/json",
                success: function (data) {
                    console.log(data);
                    grid.setData(data);
                },
                error: function (data) {
                    mini.alert('请求失败')
                }
            })
        }else{
            mini.alert('请选择需要搜索的单据编码');
        }
    }else{
        mini.alert('请选择需要查询的列')
    }
}
